<article id="wikiArticle">
<div>
<div> <div class="blockIndicator nonStandard nonStandardHeader">
<p><strong><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> 非标准</strong><br/>
      该特性是非标准的，请尽量不要在生产环境中使用它！</p>
</div></div>
</div>
<p><strong><code>toSource()</code></strong>方法返回一个表示对象源代码的字符串。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code><em>Object.toSource(); obj</em>.toSource()</code></code></pre>
<h3 id="Parameters" name="Parameters">返回值</h3>
<p>一个表示对象的源代码的字符串。</p>
<h2 id="Description" name="Description">描述</h2>
<p><code>toSource()</code>方法返回以下值:</p>
<ul>
<li>对于内置的<a href="Reference/Global_Objects/Object" title="Object 构造函数创建一个对象包装器。"><code>Object</code></a> 对象, <code>toSource</code>返回了下面的字符串，表示源码没法获取：</li>
</ul>
<pre><code  class="language-javascript">function Object() {
   [native code]
}</code></pre>
<ul>
<li>对于<a href="Reference/Global_Objects/Object" title="Object 构造函数创建一个对象包装器。"><code>Object</code></a>的实例，<code>toSource()</code> 会返回该实例源代码的字符串表示。</li>
</ul>
<p>在调试时,你可以通过<code>toSource()</code>来查看一个对象的内容。</p>
<h3 id="重写toSource()方法">重写toSource()方法</h3>
<p>允许对象重写<code>toSource()</code>方法。例如：</p>
<pre><code  class="language-javascript">function Person(name) {
    this.name = name;
}

Person.prototype.toSource = function Person_toSource() {
    return "new Person(" + uneval(this.name) + ")";
};

alert(new Person("Joe").toSource()); // ---&gt; new Person("Joe")</code></pre>
<h3 id="Built-in_toString_methods" name="Built-in_toString_methods">内置<code>的toSource方法</code></h3>
<p>每个JavaScript核心类型都有它自己的<code>toSource()</code>方法.这些对象是：</p>
<ul>
<li><a href="Reference/Global_Objects/Array/toSource" title="返回一个字符串,代表该数组的源代码."><code>Array.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> object.</li>
<li><a href="Reference/Global_Objects/Boolean/toSource" title="toSource（）方法返回一个表示对象的源码的字符串。"><code>Boolean.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Boolean" title="此页面仍未被本地化, 期待您的翻译!"><code>Boolean</code></a> object.</li>
<li><a href="Reference/Global_Objects/Date/toSource" title="toSource() 返回表示源代码的字符串。"><code>Date.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Date" title="此页面仍未被本地化, 期待您的翻译!"><code>Date</code></a> object.</li>
<li><a href="Reference/Global_Objects/Function/toSource" title="返回函数的源代码的字符串表示."><code>Function.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Function" title="此页面仍未被本地化, 期待您的翻译!"><code>Function</code></a> object.</li>
<li><a href="Reference/Global_Objects/Number/toSource" title="toSource() 方法返回该对象源码的字符串表示。"><code>Number.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> object.</li>
<li><a href="Reference/Global_Objects/RegExp/toSource" title="返回一个字符串,代表当前对象的源代码"><code>RegExp.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a> object.</li>
<li><a class="new" href="Reference/Global_Objects/SIMD/toSource" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>SIMD.%type%.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a class="new" href="Reference/SIMD" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>SIMD</code></a> objects.</li>
<li><a href="Reference/Global_Objects/String/toSource" title="toSource() 方法返回一个代表对象的源代码。"><code>String.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a> object.</li>
<li><a href="Reference/Global_Objects/Symbol/toSource" title="toSource() 方法返回代表该对象源码的字符串。"><code>Symbol.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> — <a href="Reference/Global_Objects/Symbol" title='Symbol()函数会返回symbol类型的值，该类型具有静态属性和静态方法。它的静态属性会暴露几个内建的成员对象；它的静态方法会暴露全局的symbol注册，且类似于内建对象类，但作为构造函数来说它并不完整，因为它不支持语法："new Symbol()"。'><code>Symbol</code></a> object.</li>
<li><code>Math.toSource()</code> — Returns the String "Math".</li>
</ul>
<h3 id="循环引用限制">循环引用限制</h3>
<p><span id="noHighlight_0.3266603437527873">对于包含对自身的引用的对象 (例如, 循环链表或可以遍历两种方式的树), </span><code>toSource()</code><span>不会重新创建自引用, 如火狐24。例如:</span></p>
<pre><code  class="language-javascript">var obj1 = {};
var obj2 = { a: obj1 };
obj1.b = obj2;

console.log('Cyclical: ' + (obj1.b.a == obj1));

var objSource = obj1.toSource(); // returns "({b:{a:{}}})"

obj1 = eval(objSource);

console.log('Cyclical: ' + (obj1.b.a == obj1));</code></pre>
<p><span id="noHighlight_0.30716570898096807">如果使用循环结构, 并且需要 </span><code>toSource()</code><span>, 则对象必须提供对 </span><code>toSource()</code><span> 的重写,  无论是对构造函数的引用还是提供匿名函数。</span></p>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_toSource" name="Example:_Using_toSource">使用<code>toSource()</code></h3>
<p>下面的代码定义了一个<code>Dog</code>对象类型还创建了一个<code>Dog</code>类型的对象实例<code>theDog</code>：</p>
<pre><code class="language-js">function Dog(name, breed, color, sex) {
   this.name = name;
   this.breed = breed;
   this.color = color;
   this.sex = sex;
}

theDog = new Dog("Gabby", "Lab", "chocolate", "girl");</code></pre>
<p>在<code>theDog</code>上调用<code>toSource</code>方法会显示出能定义该对象的源码：</p>
<pre><code  class="language-javascript">theDog.toSource();
// returns ({name:"Gabby", breed:"Lab", color:"chocolate", sex:"female"})</code></pre>
<h2 id="特性">特性</h2>
<p>不属于任何标准的一部分。在JavaScript1.3中实现。</p>
<h2 id="浏览器兼容">浏览器兼容</h2>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>toSource</code> <div class="bc-icons"><abbr class="only-icon" title="Deprecated. Not for use in new websites."><span>Deprecated</span><i class="ic-deprecated"></i></abbr><abbr class="only-icon" title="Non-standard. Expect poor cross-browser support."><span>Non-standard</span><i class="ic-non-standard"></i></abbr></div></th><td class="bc-supports-no bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              1</td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              4</td><td class="bc-supports-no bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
                  
                </abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
<abbr class="bc-level bc-level-no only-icon" title="No support">
<span>No support</span>
                  
                </abbr></span></dt><dd>No support</dd><dt><abbr class="only-icon" title="Non-standard. Expect poor cross-browser support."><span>Non-standard. Expect poor cross-browser support.</span><i class="ic-non-standard"></i></abbr></dt><dd>Non-standard. Expect poor cross-browser support.</dd><dt><abbr class="only-icon" title="Deprecated. Not for use in new websites."><span>Deprecated. Not for use in new websites.</span><i class="ic-deprecated"></i></abbr></dt><dd>Deprecated. Not for use in new websites.</dd></dl></section></div><p></p>
<h2 id="See_Also" name="See_Also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>Object.prototype.toString()</code></a></li>
</ul>
<p></p>
</article>